﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;

namespace RunUOGenerator
{
    public partial class frmStartup : Form
    {
        // Create a config reader.
        private ConfigReader reader = new ConfigReader();

        private int ImageWidth = 44;
        private int ImageHeight = 44;

        private bool alreadyClicked = false;

        public frmStartup()
        {
            InitializeComponent();
            buttonPanel.ControlAdded +=new ControlEventHandler(buttonPanel_ControlAdded);
        }

        private void frmStartup_Load(object sender, EventArgs e)
        {
            // Build the modules.
            Program.Modules = reader.BuildModules();

            // Sort through the array and build our interface.
            foreach (Module module in Program.Modules)
            {
                if (module.StartupImage != null)
                {
                    // Create a StartipImage class.
                    StartupImage image = new StartupImage();

                    // Add the image and give it a size.
                    image.Image = module.StartupImage;
                    image.Size = new Size(ImageWidth, ImageHeight);

                    // Give the startup image the module title and description.
                    image.Title = module.Title;
                    image.Description = module.Description;
                    image.Module = module;

                    // Create out events.
                    image.MouseLeave += new System.EventHandler(this.moduleImageMovedLeave);
                    image.MouseHover += new System.EventHandler(this.moduleImageMovedEnter);
                    image.MouseClick += new System.Windows.Forms.MouseEventHandler(this.moduleImageClicked);

                    // Move the title to the panel.
                    MoveToPanel(image);
                }                
            }
        }

        /// <summary>
        /// Caputure the event when the mouse is moved over the image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void moduleImageMovedEnter(object sender, EventArgs e)
        {
            StartupImage si = (StartupImage)sender;

            lblDescription.Visible = false;
            lblDescription.Text = si.Description;

            // Center
            lblDescription.Left = (this.Width - lblDescription.Width) / 2;

            // Now show
            lblDescription.Visible = true;
        }

        /// <summary>
        /// Caputure the event when the mouse is moved away the image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void moduleImageMovedLeave(object sender, EventArgs e)
        {
            lblDescription.Text = "";
        }

        /// <summary>
        /// Caputure the event when the image is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void moduleImageClicked(object sender, MouseEventArgs e)
        {
            StartupImage si = (StartupImage)sender;

            if (!alreadyClicked)
            {
                alreadyClicked = true;

                frmMain main = new frmMain();
                main.Module = si.Module;

                // Hide this form, show the main.
                this.Hide();
                main.Show();
            }
        }

        /// <summary>
        /// This mehtod moves the control to the panel and sets the location.
        /// </summary>
        /// <param name="image"></param>
        private void MoveToPanel(StartupImage image)
        {
            int x = buttonPanel.Controls.Count * ImageWidth;

            this.buttonPanel.Controls.Add(image);
            image.Location = new Point(x, 0);
        }

        /// <summary>
        /// Capture the event when a control is added into the buttonPanel. Adjust the width. Make it dynamic.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonPanel_ControlAdded(object sender, EventArgs e)
        {
            int boxWidth = buttonPanel.Controls.Count * ImageWidth;
            int formWidth = this.Width;

            buttonPanel.Width = boxWidth;

            // Center
            buttonPanel.Left = (formWidth - boxWidth) / 2;
            
        }
    }
}
